<?php
/**
 * Description of Logger
 *
 * @author joona
 */
abstract class Core_Logger implements Core_Defines
{
    protected static $logTarget = null;
    protected static $logHandle;

    /**
     * Initialize logger, set logfile to write to
     * Logfile is desiced with priorities:
     * 1) Given target
     * 2) Class variable $logTarget
     * 3) SYSTEM_LOG in Application.ini
     * 
     * @param string $target Absolute path to log file to write to (optional)
     */
    protected static function initLogger($target = null)
    {
        self::$logTarget = is_null($target)
            ? Core_Registry::get('SYSTEM_LOG')
            : $target;
        $target = is_null(self::$logTarget)
            ? Core_Registry::get('SYSTEM_LOG')
            : self::$logTarget;
        
        self::$logHandle = fopen($target, 'a');
    }

    public static function log($message, $logTarget = null)
    {
        if(is_null($logTarget))
            self::$logHandle || self::initLogger();

        return fwrite(
            is_null($logTarget) ? self::$logHandle : fopen($logTarget, 'a'),
            date('Y-m-d H:i:s').': '.get_called_class().': '.$message."\n"
        );
    }
}
